package com.ruoyi.webServiceUtil;

import com.ruoyi.basebill.domain.WfzWebservice;
import com.ruoyi.basebill.service.IWfzWebserviceService;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.message.SOAPHeaderElement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;
import java.rmi.RemoteException;
import java.util.List;


public class WebServiceHelper {


    /**
     *
     * @param easusername 用户名
     * @param easpassword 密码
     * @param easusername 接口地址
     * @author wyx
     * @return
     */
    public static String login(String easusername,String easpassword,String loginUrl,String easdbcode) throws ServiceException, RemoteException {

        String easslnname = "eas",easlanguage = "L2";

        int easdbtype = 1;


        Service service = new Service();
        Call call = (Call) service.createCall();
        call.setOperationName("login");
        call.setTargetEndpointAddress(loginUrl);
        call.setReturnType(new QName("urn:client", "WSContext"));
        call.setReturnClass(WSContext.class);
        call.setReturnQName(new QName("", "loginReturn"));
        // 超时
        call.setTimeout(Integer.valueOf(1000*600000*60));
        call.setMaintainSession(true);
        WSContext ctx=(WSContext)call.invoke(new Object[]{easusername,easpassword, easslnname,easdbcode,easlanguage,easdbtype});
        String sessionId = ctx.getSessionId();
        call.clearOperation();
        return sessionId;
    }

    /**
     *
     * @param sessionId sessionId
     * @param endpoint 接口地址
     * @param json json 参数
     * @param method 具体方法名
     * @return
     * @throws ServiceException
     * @throws RemoteException
     */
    public static String serviceInterface(String sessionId,String endpoint,String method,String json,String jkNum) throws ServiceException, RemoteException {
        Service service = new Service();
        Call call = (Call) service.createCall();

        call.setTargetEndpointAddress(endpoint);
        call.setOperationName(new QName("", method));    //
        call.addParameter("in",  org.apache.axis.encoding.XMLType.XSD_STRING,  javax.xml.rpc.ParameterMode.IN);
        // 接口的参数
        call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);																								// 设置返回类型

        // 给方法传递参数，并且调用方法
        SOAPHeaderElement header = new SOAPHeaderElement("http://login.webservice.bos.kingdee.com", "SessionId", sessionId);
        call.addHeader(header);

        String result = (String) call.invoke(new Object[] {json,jkNum});
        return result;
    }
}
